package wikitools.servlet;

import javax.servlet.http.HttpServlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ReportDownload extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        InputStream is = getServletContext().
            getResourceAsStream("/report/433620-wikiblog.pdf");
        if(is == null){
            
            resp.getWriter().print("This file does not exist!");
            return;
        }
        resp.setContentType("text/pdf");
        resp.setHeader("Content-Disposition","attachment; filename=433620-wikiblog.pdf");
            
        int read = 0;
        byte[] bytes = new byte[1024];
        
        OutputStream os = resp.getOutputStream();
        while((read=is.read(bytes)) != -1){
            os.write(bytes, 0, read);
        }
        os.flush();
        os.close();
        
    }
    

}
